package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it.reggie.annotation.Log;
import com.it.reggie.commen.R;
import com.it.reggie.entity.User;
import com.it.reggie.service.UserService;
import com.it.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
@SuppressWarnings(value = "all")
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private RedisTemplate redisTemplate;

    //获取验证码
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user, HttpServletRequest request) {

        String phone = user.getPhone();

        String integer = ValidateCodeUtils.generateValidateCode(4).toString();

        log.info(String.valueOf(integer));

        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getPhone, phone);
        User one = userService.getOne(qw);

        if (one == null) {

            userService.save(user);

        }

//        HttpSession session = request.getSession();
//
//        session.setAttribute(phone, integer);

        ValueOperations valueOperations = redisTemplate.opsForValue();
        //将生成的验证码存储到redis中，设置存活时间一分钟
        valueOperations.set("code", integer, 5, TimeUnit.MINUTES);

        return R.success("发送成功");

    }

    //登录
    @Log("用户进行了登录的操作")
    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpServletRequest request) {

        String phone = (String) map.get("phone");
        String code = (String) map.get("code");

        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();

        qw.eq(User::getPhone, phone);

        User one = userService.getOne(qw);

//      Integer attribute = (Integer) request.getSession().getAttribute(phone);

        //从redis中获取验证码
        String code1 = (String) redisTemplate.opsForValue().get("code");

        if (one != null && code.equals(code1)) {
            Long id = one.getId();
            request.getSession().setAttribute("user", id);
            //登录成功删除验证码
            redisTemplate.delete("code");
            return R.success("登录成功");
        } else {
            return R.error("登录失败");
        }

    }

    //退出
    @Log("用户进行了退出的操作")
    @PostMapping("/loginout")
    public R loginout(HttpServletRequest request) {

        request.getSession().removeAttribute("user");

        return R.success("退出成功");
    }

}
